package com.myl.service.store.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myl.dao.store.QuestionDao;
import com.myl.domain.store.Question;
import com.myl.factory.MapperFactory;
import com.myl.service.store.QuestionService;
import com.myl.utils.TransactionUtil;

public class QuestionServiceImp implements QuestionService{

	@Override
	public String save(Question question,boolean flag) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
			//id使用uuid的生成策略来获取
			String id =UUID.randomUUID().toString();
			question.setId(id);
			//默认未审核，创建时间
			question.setReviewStatus("0");
			question.setCreateTime(new Date());
			if(flag){
				//为图片名字设置id
				question.setPicture(id);
			}
			
			
			questionDao.save(question);
			
			TransactionUtil.commit(sqlSession);
			return id;
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
		
	}
	
	@Override
	public void delete(Question question) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);				
			questionDao.delete(question);
			TransactionUtil.commit(sqlSession);
			
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public void update(Question question,boolean flag) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);		
			
			if(flag){
				question.setPicture(question.getId());
			}
			questionDao.update(question);
			
			TransactionUtil.commit(sqlSession);
			
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public Question findById(String id) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);		
			return questionDao.findById(id);
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
		
	}

	@Override
	public List<Question> findAll() {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);		
			
			List<Question> all = questionDao.findAll();
			return all;
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}
	
	@Override
	public PageInfo findAll(int page, int size) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);		
			
			PageHelper.startPage(page,size);
			List<Question> all = questionDao.findAll();
			PageInfo  pageInfo = new PageInfo(all);
			return pageInfo;
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	}

	@Override
	public ByteArrayOutputStream getReport() throws IOException {
		
		
		Workbook workbook = new XSSFWorkbook();
		Sheet s = workbook.createSheet("题目数据文件");		
		//参数表示第几行到第几行，第几列到第几列
				s.addMergedRegion(new CellRangeAddress(1, 1, 1, 12));
		//设置通用配置
		//s.setColumnWidth(4, 100);
				CellStyle cS_field = workbook.createCellStyle();
				cS_field.setAlignment(HorizontalAlignment.CENTER);
				
				
				
		//制作标题
		Row row = s.createRow(1);
		Cell cell_1_11 = row.createCell(1);
		cell_1_11.setCellValue("在线试题导出信息");
		//创建一个样式  水平垂直居中
		CellStyle cS = workbook.createCellStyle();
		cS.setAlignment(HorizontalAlignment.CENTER);
		cS.setVerticalAlignment(VerticalAlignment.CENTER);
		
		/*cS.setBorderTop(BorderStyle.THIN);
		cS.setBorderLeft(BorderStyle.THIN);
		cS.setBorderRight(BorderStyle.THIN);
		cS.setBorderBottom(BorderStyle.THIN);*/
		cell_1_11.setCellStyle(cS);
		
		
		//制作表头
		String[] fields = {"题目ID","所属公司ID","所属目录ID","题目简介",	"题干描述","题干配图",
				"题目分析","题目类型","题目难度","是否经典题","题目状态","审核状态"};
		
		Row row_2 = s.createRow(2);
		for(int i=0;i<fields.length;i++){
		Cell cell_2_temp = row_2.createCell(1+i);
		cell_2_temp.setCellValue(fields[i]);
		
		cell_2_temp.setCellStyle(cS_field);
		}
		
		//制作数据区
		SqlSession sqlSession = null;
		List<Question> questionList = null;
		try {
			sqlSession = MapperFactory.getSqlSession();
			QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);		

			questionList = questionDao.findAll();
		} catch (Exception e) {
			TransactionUtil.rollback(sqlSession);
			throw new RuntimeException(e);
			
		}finally{
			TransactionUtil.close(sqlSession);
		}
	 
		 
		   int row_index =0;		
		  for(Question q:questionList){
		   int cell_index =0;
		   Row row_temp = s.createRow(3+row_index++);
		   Cell cell_data_1 = row_temp.createCell(1+cell_index++);
		   cell_data_1.setCellValue(q.getId());
		   cell_data_1.setCellStyle(cS_field);
		   
		   Cell cell_data_2 = row_temp.createCell(1+cell_index++);
		   cell_data_2.setCellValue(q.getCompanyId());
		   cell_data_2.setCellStyle(cS_field);
		   
		   Cell cell_data_3 = row_temp.createCell(1+cell_index++);
		   cell_data_3.setCellValue(q.getCatalogId());
		   cell_data_3.setCellStyle(cS_field);
		   
		   Cell cell_data_4 = row_temp.createCell(1+cell_index++);
		   cell_data_4.setCellValue(q.getRemark());
		   cell_data_4.setCellStyle(cS_field);
		   
		   Cell cell_data_5 = row_temp.createCell(1+cell_index++);
		   cell_data_5.setCellValue(q.getSubject());
		   cell_data_5.setCellStyle(cS_field);
		   
		   Cell cell_data_6= row_temp.createCell(1+cell_index++);
		   cell_data_6.setCellValue(q.getPicture());
		   cell_data_6.setCellStyle(cS_field);
		   
		   Cell cell_data_7 = row_temp.createCell(1+cell_index++);
		   cell_data_7.setCellValue(q.getAnalysis());
		   cell_data_7.setCellStyle(cS_field);
		   
		   Cell cell_data_8 = row_temp.createCell(1+cell_index++);
		   cell_data_8.setCellValue(q.getType());
		   cell_data_8.setCellStyle(cS_field);
		   
		   Cell cell_data_9 = row_temp.createCell(1+cell_index++);
		   cell_data_9.setCellValue(q.getDifficulty());
		   cell_data_9.setCellStyle(cS_field);
		   
		   Cell cell_data_10 = row_temp.createCell(1+cell_index++);
		   cell_data_10.setCellValue(q.getIsClassic());
		   cell_data_10.setCellStyle(cS_field);
		   
		   Cell cell_data_11 = row_temp.createCell(1+cell_index++);
		   cell_data_11.setCellValue(q.getState());
		   cell_data_11.setCellStyle(cS_field);
		   
		   Cell cell_data_12 = row_temp.createCell(1+cell_index++);
		   cell_data_12.setCellValue(q.getReviewStatus());
		   cell_data_12.setCellStyle(cS_field);
		}

		ByteArrayOutputStream os = new ByteArrayOutputStream();
		workbook.write(os);
		workbook.close();
		return os;
		
		
		
	}

}
